package com.guojian.http.handle.service;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.smartboot.http.HttpRequest;
import org.smartboot.http.HttpResponse;
import org.smartboot.http.server.handle.HttpHandle;

import com.guojian.common.http.HttpUtils;
import com.guojian.http.handle.HttpSmartRequestBuilder;
import com.guojian.http.handle.HttpSmartResponseWrapper;
import com.guojian.http.handle.route.HttpSmartRouteHandle;

public class HttpSmartServiceHandle extends HttpHandle {

	private String targetUrl;//目标URL

	@Override
	public void doHandle(HttpRequest request, HttpResponse response) throws Exception {
		//http://localhost:9080/xx?deptId=1&a=2 -->getQueryString = deptId=1&a=2
		if (StringUtils.isNotBlank(request.getQueryString())) {
			targetUrl += "?" + request.getQueryString();
		}

		//针对application/x-www-form-urlencoded做特殊设置,因为服务器底层对表单提交的content-type设置了getInputStream = empty,
		//所以需要手工获取参数组装UrlEncodedFormEntity
		//提前读取了inputstream来获取参数组装FormUrlencoded,导致后续继续读取inputstream直接返回-1,所以设置inputStream = EMPTY_INPUT_STREAM;
		/* public InputStream getInputStream() throws IOException {
		        if (inputStream != null) {
		            return inputStream;
		        }
		        int contentLength = getContentLength();
		        if (contentLength <= 0 || request.getFormUrlencoded() != null) {
		            inputStream = EMPTY_INPUT_STREAM;
		        } else {
		            inputStream = new PostInputStream(request.getAioSession().getInputStream(contentLength), contentLength);
		        }
		        return inputStream;
		    }*/

		System.out.println("开始转发: [" + targetUrl + "]");
		//Future<org.apache.http.HttpResponse> responseFuture = HttpUtils.doAsyncSmartHttpProcess(HttpRequestBuilder.create().request(request).targetUrl(targetUrl).build().buildRequest());

		CloseableHttpResponse clientResponseData = HttpUtils.doHttpClientProcess(HttpSmartRequestBuilder.create().request(request).targetUrl(targetUrl).build().buildRequest());

		//org.apache.http.HttpResponse clientResponseData = responseFuture.get();

		if (clientResponseData == null) {
			HttpSmartRouteHandle.ERROR_HANDLE.doHandle(request, response);
		} else {
			/*int statusCode = clientResponseData.getStatusLine().getStatusCode();
			if (statusCode == 302) {
				String redirectLocation = clientResponseData.getFirstHeader(HttpHeaders.LOCATION).getValue();
				URL redirectUrl = new URL(redirectLocation);
				URL oldTargetUrl = new URL(targetUrl);
				this.targetUrl = oldTargetUrl.getProtocol() + "://" + oldTargetUrl.getAuthority() + redirectUrl.getPath();
				doHandle(request, response);
				return;
			}*/
			new HttpSmartResponseWrapper(response, request, clientResponseData).writeResponse();
		}

	}

	public String getTargetUrl() {
		return targetUrl;
	}

	public HttpSmartServiceHandle setTargetUrl(String targetUrl) {
		this.targetUrl = targetUrl;
		return this;
	}

	public static void main(String[] args) {

		String aa = "bcauft-8";
		System.out.println(aa.substring(0, aa.indexOf(";")));

	}
}
